package com.itfreer.ui.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;

import com.itfreer.ui.R;
import com.itfreer.utils.StringUtils;
import com.itfreer.ui.base.api.IPower;
import com.itfreer.ui.base.api.ISimpleView;

/**
 * 定义基于TextView的toggle按钮
 */
public class ToggleTextView extends TextView implements ISimpleView, View.OnClickListener, IPower {

    public interface OnChecked {
        void onChecked(View v, boolean isChecked);
    }
    
    private String fieldName;
    private Object fieldValue;
    private boolean checked = false;
    private int offColor;
    private int onColor;
    private OnChecked OnChecked;
    protected String checkedString = "";
    protected String unCheckedString = "";

    public void setOnChecked(OnChecked OnChecked){
        this.OnChecked = OnChecked;
    }

    public ToggleTextView(Context context) {
        super(context);

        onColor = getCurrentTextColor();

        this.setChecked(false);
        this.setOnClickListener(this);
    }

    public ToggleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttrs(context, attrs);
        onColor = getCurrentTextColor();
        this.setChecked(false);
        this.setOnClickListener(this);
    }

    public ToggleTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context, attrs);
        onColor = getCurrentTextColor();

        this.setChecked(false);
        this.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        boolean isChecked = getChecked();
        isChecked = !isChecked;
        setChecked(isChecked);
    }

    protected void getAttrs(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleView);
        fieldName = a.getString(R.styleable.SimpleView_fieldName);
        offColor = a.getColor(R.styleable.SimpleView_offColor, 0);

        checkedString = a.getString(R.styleable.SimpleView_checkedString);
        if(StringUtils.IsNullOrEmpty(checkedString)){
            checkedString = context.getString(R.string.icon_swith_on);
        }

        unCheckedString = a.getString(R.styleable.SimpleView_unCheckedString);
        if(StringUtils.IsNullOrEmpty(unCheckedString)){
            unCheckedString = context.getString(R.string.icon_swith_off);
        }
    }

    public void setChecked(boolean checked){
        this.checked = checked;
        if(checked){
            this.setText(checkedString);
            this.setTextColor(onColor);
        } else {
            this.setText(unCheckedString);
            this.setTextColor(offColor);
        }
        if(OnChecked != null){
            OnChecked.onChecked(this, checked);
        }
    }

    public boolean getChecked(){
        return checked;
    }

    /**
     * 设置绑定的依据对象
     * @param name
     */
    @Override
    public void setFieldName(String name){
        this.fieldName = name;
    }

    /**
     * 获取绑定的依据对象
     * @return
     */
    @Override
    public String getFieldName(){
        return fieldName;
    }

    /**
     * 设置绑定的值
     * @param value
     */
    @Override
    public void setFieldValue(Object value){

        this.fieldValue = value;
        if(fieldValue!=null && fieldValue.equals("1")){
            this.setChecked(true);
        }else{
            this.setChecked(false);
        }
        /*this.fieldValue = value;
        if(fieldValue!=null && (boolean)fieldValue){
            this.setChecked(true);
        }else{
            this.setChecked(false);
        }*/
    }

    /**
     * 获取当前的值
     * @return
     */
    @Override
    public Object getFieldValue(){
        fieldValue = getChecked();
        if(fieldValue.toString().equals("true")){
            fieldValue="1";
        }
        else{
            fieldValue="0";
        }
        return fieldValue;
    }
}